package com.exercise.widgets.recyclerview.table

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView
import com.exercise.struct.base.BaseActivity
import com.exercise.widgets.databinding.ActivityTableBinding
import com.exercise.widgets.recyclerview.table.adapter.TableAdapter
import com.exercise.widgets.recyclerview.table.viewmodel.TableViewModel

class TableActivity : BaseActivity<TableViewModel, ActivityTableBinding>() {
    override val bindingInflater: (LayoutInflater) -> ActivityTableBinding
        get() = ActivityTableBinding::inflate
    override val modelClass: Class<TableViewModel>
        get() = TableViewModel::class.java
    private val mAdapter by lazy {
        TableAdapter()
    }

    override fun initData() {
        mViewModel.loadData()
    }

    override fun initView() {

        initRecyclerView()
        initObserver()
    }

    private fun initObserver() {
        mViewModel.onDataChanged.observe(this) {
            mAdapter.setList(it)
        }
    }

    private fun initRecyclerView() {
        binding.recyclerView.run {
            adapter = mAdapter
        }
    }

    companion object {
        fun start(context: Context) {
            Intent(context, TableActivity::class.java)
                .also {
                    context.startActivity(it)
                }
        }
    }
}